﻿Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.Reporting.WebForms
Imports log4net

Partial Class Report_RP19
    Inherits System.Web.UI.Page

    Public ScriptText As String
    Dim Report As Object
    Private Shared logger As ILog = LogManager.GetLogger("Report_RP19")

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Getdata()
    End Sub
    Function Getdata() As Decimal
        Dim adapter As New Data_Report3TableAdapters.RP19TableAdapter
        Dim dt As New Data_Report3.RP19DataTable
        Dim sql As String

        sql = ""
        sql = " SELECT SUBSTRING(bd.Project_ProjectCode, 1, 1) AS Code, "
        sql += " (CASE WHEN SUBSTRING(bd.[Project_ProjectCode], 1, 1) = 'P' THEN 'งานโครงการ' "
        sql += " WHEN SUBSTRING(bd.[Project_ProjectCode], 1, 1) = 'D' THEN 'งานสนับสนุน' END) AS CodeName, "
        sql += " (CASE WHEN pj.FundCenter_FundCenterCode IS NULL THEN 'โครงการกลาง' "
        sql += " ELSE 'โครงการภาค/ฝ่าย' END) AS ProjectType, "
        sql += " bd.FundCenter_FundCenterCode AS FundCenterCode, "
        sql += " fc.Description AS FundCenterName, bd.Project_ProjectCode AS ProjectCode, "
        sql += " pj.ShortDescription AS ProjectNameShort, pj.Description AS ProjectName, "
        sql += " (SELECT SUM(Amount) AS Expr1"
        sql += " FROM BudgetDetails AS bd1"
        sql += " WHERE (Project_ProjectCode = bd.Project_ProjectCode) AND            "
        sql += " (Project_PlanYear = " & Session("year") & ") AND "
        sql += " (FundCenter_FundCenterCode = bd.FundCenter_FundCenterCode) AND         "
        sql += " (FundCenter_PlanYear = " & Session("year") & ") AND (Version = '" & Session("version") & "')) "
        sql += " AS Amount"
        sql += " FROM BudgetDetails AS bd "
        sql += " LEFT OUTER JOIN FundCenters AS fc ON bd.FundCenter_FundCenterCode = fc.FundCenterCode "
        sql += " AND bd.FundCenter_PlanYear = fc.PlanYear "
        sql += " LEFT OUTER JOIN Projects AS pj ON bd.Project_ProjectCode = pj.ProjectCode "
        sql += " AND bd.Project_PlanYear = pj.PlanYear"
        sql += " WHERE (bd.Project_ProjectCode IS NOT NULL OR bd.Project_ProjectCode <> '') "
        sql += " AND (bd.Project_PlanYear IS NOT NULL OR bd.Project_PlanYear <> '')"
        sql += " AND bd.version = '" & Session("version") & "' "
        sql += " AND bd.planyear = " & Session("year") & " "
        If Session("FundcenterAll") <> "" Then
            sql += " AND bd.FundCenter_FundCenterCode in (" & Session("FundcenterAll") & ") "
            sql += " AND bd.FundCenter_planyear = " & Session("year") & " "
        End If
        If Session("code") <> "" Then
            sql += " AND SUBSTRING(bd.Project_ProjectCode, 1, 1) = '" & Session("code") & "' "
        End If
        If Session("projectCode") <> "" Then
            sql += " AND bd.Project_ProjectCode like '%" & Session("projectCode") & "%'"
        End If
        sql += " GROUP BY pj.FundCenter_FundCenterCode, bd.FundCenter_FundCenterCode, "
        sql += " fc.Description, bd.Project_ProjectCode, pj.ShortDescription, "
        sql += " pj.Description "
        sql += " ORDER BY Code DESC "

        adapter.GetDataByFilter(dt, sql)

        Dim das As New DataSet
        das.DataSetName = "R19DS"
        das.Tables.Add(dt)

        ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", das.Tables("RP19")))
        ReportViewer1.DocumentMapCollapsed = True

        Dim UserName As String = Session("users")
        Dim years As String = Session("year")
        Dim version As String = Session("version")
        Dim users As String = Session("users")
        Dim p0, p1, p2, p3 As ReportParameter
        p1 = New ReportParameter("year", years)
        p0 = New ReportParameter("year1", years - 1)
        p2 = New ReportParameter("version", version)
        p3 = New ReportParameter("username", UserName)
        ReportViewer1.LocalReport.SetParameters(p0)
        ReportViewer1.LocalReport.SetParameters(p1)
        ReportViewer1.LocalReport.SetParameters(p2)
        ReportViewer1.LocalReport.SetParameters(p3)
        ReportViewer1.LocalReport.Refresh()
        Session("count") = dt.Rows.Count
        Return dt.Rows.Count
    End Function
    Public Function nPageNumber() As String
        Dim str As String
        str = Me.Report.Globals!PageNumber.ToString()
        Return str
    End Function
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            ScriptText = "<script> wclose();</script>"
        End If
    End Sub
End Class
